set(WEBVIEW_ARCHIVE_SYSTEM_ARCH_FILE_NAME "@WEBVIEW_ARCHIVE_SYSTEM_ARCH_FILE_NAME@")
set(WEBVIEW_ARCHIVE_UNIVERSAL_FILE_NAME "@WEBVIEW_ARCHIVE_UNIVERSAL_FILE_NAME@")
set(WEBVIEW_PACKAGE_AMALGAMATION "@WEBVIEW_PACKAGE_AMALGAMATION@")
set(WEBVIEW_PACKAGE_DOCS "@WEBVIEW_PACKAGE_DOCS@")
set(WEBVIEW_PACKAGE_HEADERS "@WEBVIEW_PACKAGE_HEADERS@")
set(WEBVIEW_PACKAGE_LIB "@WEBVIEW_PACKAGE_LIB@")
set(CMAKE_INSTALL_DOCDIR "@CMAKE_INSTALL_DOCDIR@")

function(create_archive_for_components)
    # Parse function arguments
    set(COMPONENT_INDEX 0)
    set(ARG_INDEX 0)
    while(ARG_INDEX LESS "${ARGC}")
        set(ARG "${ARGV${ARG_INDEX}}")
        if(ARG STREQUAL "ARCHIVE")
            math(EXPR ARG_INDEX "${ARG_INDEX}+1")
            set(ARCHIVE "${ARGV${ARG_INDEX}}")
        elseif(ARG STREQUAL "COMPONENT")
            math(EXPR SUB_ARGC "${ARG_INDEX}+4")
            while(ARG_INDEX LESS "${SUB_ARGC}")
                set(SUB_ARG "${ARGV${ARG_INDEX}}")
                if(SUB_ARG STREQUAL "NAME")
                    math(EXPR ARG_INDEX "${ARG_INDEX}+1")
                    set("COMPONENT${COMPONENT_INDEX}_NAME" "${ARGV${ARG_INDEX}}")
                elseif(SUB_ARG STREQUAL "SUBDIR")
                    math(EXPR ARG_INDEX "${ARG_INDEX}+1")
                    set("COMPONENT${COMPONENT_INDEX}_SUBDIR" "${ARGV${ARG_INDEX}}")
                endif()
                math(EXPR ARG_INDEX "${ARG_INDEX}+1")
            endwhile()
            math(EXPR COMPONENT_INDEX "${COMPONENT_INDEX}+1")
            continue()
        else()
            message(FATAL_ERROR "Unparsed arg ${ARG_INDEX}: ${ARGV${ARG_INDEX}}")
        endif()
        math(EXPR ARG_INDEX "${ARG_INDEX}+1")
    endwhile()
    math(EXPR COMPONENT_COUNT "${COMPONENT_INDEX}")

    # Build list of component names
    set(COMPONENT_NAMES)
    set(COMPONENT_INDEX 0)
    while(COMPONENT_INDEX LESS "${COMPONENT_COUNT}")
        list(APPEND COMPONENT_NAMES "${COMPONENT${COMPONENT_INDEX}_NAME}")
        math(EXPR COMPONENT_INDEX "${COMPONENT_INDEX}+1")
    endwhile()

    string(SHA256 NAME_HASH "${COMPONENT_NAMES}")
    set(TEMP_DIR "${CPACK_TEMPORARY_DIRECTORY}/${NAME_HASH}")

    # Consolidate files from each component's staging directory
    set(COMPONENT_INDEX 0)
    while(COMPONENT_INDEX LESS "${COMPONENT_COUNT}")
        set(COMPONENT_NAME "${COMPONENT${COMPONENT_INDEX}_NAME}")
        set(COMPONENT_SUBDIR "${COMPONENT${COMPONENT_INDEX}_SUBDIR}")
        if("${COMPONENT_SUBDIR}" STREQUAL "")
            set(COMPONENT_SUBDIR ".")
        endif()
        file(GLOB_RECURSE COMPONENT_FILES
            RELATIVE "${CPACK_TEMPORARY_DIRECTORY}/${COMPONENT_NAME}/${COMPONENT_SUBDIR}"
            "${CPACK_TEMPORARY_DIRECTORY}/${COMPONENT_NAME}/${COMPONENT_SUBDIR}/**")
        foreach(COMPONENT_FILE IN LISTS COMPONENT_FILES)
            get_filename_component(COMPONENT_DIR "${TEMP_DIR}/${COMPONENT_FILE}" DIRECTORY)
            file(COPY "${CPACK_TEMPORARY_DIRECTORY}/${COMPONENT_NAME}/${COMPONENT_SUBDIR}/${COMPONENT_FILE}" DESTINATION "${COMPONENT_DIR}")
        endforeach()
        math(EXPR COMPONENT_INDEX "${COMPONENT_INDEX}+1")
    endwhile()

    # Create archive
    file(GLOB_RECURSE TEMP_FILES RELATIVE "${TEMP_DIR}" "${TEMP_DIR}/**")
    execute_process(COMMAND ${CMAKE_COMMAND} -E tar cz "${ARCHIVE}" ${TEMP_FILES}
        WORKING_DIRECTORY "${TEMP_DIR}")
endfunction()

if(WEBVIEW_PACKAGE_DOCS)
    # API documentation archive
    create_archive_for_components(
        ARCHIVE "${CPACK_PACKAGE_DIRECTORY}/${WEBVIEW_ARCHIVE_UNIVERSAL_FILE_NAME}-apidoc.tar.gz"
        COMPONENT NAME webview_api_docs SUBDIR "${CMAKE_INSTALL_DOCDIR}")
endif()

if(WEBVIEW_PACKAGE_HEADERS)
    # Header archive
    create_archive_for_components(
        ARCHIVE "${CPACK_PACKAGE_DIRECTORY}/${WEBVIEW_ARCHIVE_UNIVERSAL_FILE_NAME}-headers.tar.gz"
        COMPONENT NAME webview_headers SUBDIR .)
endif()

if(WEBVIEW_PACKAGE_DOCS)
    # Per-system/architecture development archive
    set(DEV_PACKAGE_API_DOCS_ARGS COMPONENT NAME webview_api_docs SUBDIR .)
endif()

if(WEBVIEW_PACKAGE_LIB)
    # CMake configuration and compiled libraries
    create_archive_for_components(
        ARCHIVE "${CPACK_PACKAGE_DIRECTORY}/${WEBVIEW_ARCHIVE_SYSTEM_ARCH_FILE_NAME}-lib.tar.gz"
        ${DEV_PACKAGE_API_DOCS_ARGS}
        COMPONENT NAME webview_cmake SUBDIR .
        COMPONENT NAME webview_libraries SUBDIR .)
endif()

if(WEBVIEW_PACKAGE_AMALGAMATION)
    # Amalgamated library
    create_archive_for_components(
        ARCHIVE "${CPACK_PACKAGE_DIRECTORY}/${WEBVIEW_ARCHIVE_UNIVERSAL_FILE_NAME}-amalgamation.tar.gz"
        COMPONENT NAME webview_amalgamation SUBDIR .)
endif()
